package com.hmkj.web.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainer;
import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer;
import org.springframework.boot.web.servlet.ErrorPage;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpStatus;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

/**
 * Created by Donfy on 2017/5/25.
 */
@Slf4j
@Configuration
public class WebConfig extends WebMvcConfigurerAdapter {


    @Value("${com.hmkj.interceptor.uri}")
    private String[] uri;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        log.info("拦截器加载..");
        // 多个拦截器组成一个拦截器链
        // addPathPatterns 用于添加拦截规则
        registry.addInterceptor(new WebInterceptor(uri)).addPathPatterns("/**");
        //用于排除拦截的uri
        registry.addInterceptor(new WebInterceptor(uri)).excludePathPatterns("/api/member/pay/payResultNotify");
        super.addInterceptors(registry);
    }

    /**
     * 跨域CORS配置
     * @param registry
     */
//    @Override
//    public void addCorsMappings(CorsRegistry registry){
//        super.addCorsMappings(registry);
//        registry.addMapping("/cors/**")
//                .allowedHeaders("*")
//                .allowedMethods("POST","GET")
//                .allowedOrigins("*");
//    }

    // 静态资源加载
//    @Override
//    public void addResourceHandlers(ResourceHandlerRegistry registry) {
//        registry.addResourceHandler("/**").addResourceLocations("classpath:/webapps/html/p2p_huili");
//        super.addResourceHandlers(registry);
//    }

    @Bean
    public EmbeddedServletContainerCustomizer containerCustomizer() {
        return new EmbeddedServletContainerCustomizer() {
            @Override
            public void customize(ConfigurableEmbeddedServletContainer container) {
//                container.addErrorPages(new ErrorPage(HttpStatus.BAD_REQUEST, "/400"));
//                container.addErrorPages(new ErrorPage(HttpStatus.INTERNAL_SERVER_ERROR, "/500"));
                container.addErrorPages(new ErrorPage(HttpStatus.NOT_FOUND, "/404"));
            }
        };
    }

}
